package modules.citysection.top
{
import core.model.PlayerModel;

import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

import framework.proxy.ComponentProxy;
import framework.view.components.bar.ProgressBarProxy;
import framework.view.components.button.ButtonProxy;

import values.events.ItemValueEvent;
import values.item.INumbericItemValue;
import values.item.ItemTemplateKeyType;

import view.component.asset.IconProxy;
import view.interfaces.ITopUI;

/**
 * CitySectionTopUIProxy
 * @author jliang
 * 
 */
public class CitySectionTopUIProxy extends ComponentProxy implements ITopUI
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function CitySectionTopUIProxy()
	{
		super();
	}
    //==========================================================================
    //  Properties
    //==========================================================================
    public function get displayObject():DisplayObject
    {
        return this.skinContent;
    }
	//==========================================================================
	//  Variables
	//==========================================================================
	[Inject]
	public var playerModel:PlayerModel;
    
    public function get avatarIcon():IconProxy
    {
        return proxys["avatarIcon"];
    }
    
    public function get xpBar():ProgressBarProxy
    {
        return proxys["xpBar"];
    }
    
    public function get energyBar():ProgressBarProxy
    {
        return proxys["energyBar"];
    }
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================	
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"avatarIcon", classRef: IconProxy, id:"avatarIcon" },
            { skin:"settingBtn", classRef: ButtonProxy, id:"settingBtn", onClick: onClickSettingHandler },
            { skin:"giftBtn", classRef: ButtonProxy, id:"giftBtn", onClick: onClickGiftHandler },
            { skin:"xpBar", classRef: ProgressBarProxy, id:"xpBar" },
            { skin:"energyBar", classRef: ProgressBarProxy, id:"energyBar" }
		];
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		initResource("coin");
		initResource("cash");
		initResource("wood");
		initResource("metal");
		initResource("energy");
        initResource("xp");
		avatarIcon.skinContent.addEventListener(MouseEvent.CLICK , onAvatarClick);
        updateLevel();
        
        //avatarIcon.load(playerModel.myPlayerAvatar.template.getAssetByKey("icon").url);
	}
    
    override public function dispose():void
    {
		avatarIcon.skinContent.removeEventListener(MouseEvent.CLICK , onAvatarClick);
        
        super.dispose();
    }
    
    protected function updateLevel():void
    {
        skin.levelLabel.text = String(playerModel.myPlayerAvatar.level);
    }
	
	protected function onAvatarClick(evt:Event):void
	{
		Game.instance.heroUI.showHero();
	}
	
	protected function initResource(key:String):void
	{
		updateResource(key);
		
        var item:INumbericItemValue;
        
        if (key == ItemTemplateKeyType.XP)
        {
            item = playerModel.myPlayerAvatar.levelValue.resourceVO;
        }
        else
        { 
            item = playerModel.getNumbericItem(key);
        }
        item.addEventListener(ItemValueEvent.NUM_CHANGED, resourceNumChangedHandler);
	}
	
	protected function updateResource(key:String):void
	{        
        var current:Number = 0;
        if (key == ItemTemplateKeyType.ENERGY)
        {
            energyBar.minValue = 0;
            energyBar.maxValue = playerModel.myPlayerAvatar.maxEnergy;
            current = energyBar.currentValue = playerModel.myPlayerAvatar.energy;
        }
        else if (key == ItemTemplateKeyType.XP)
        {
            xpBar.minValue = playerModel.myPlayerAvatar.levelValue.prevResource;
            xpBar.maxValue = playerModel.myPlayerAvatar.levelValue.nextResource;
            current = xpBar.currentValue = playerModel.myPlayerAvatar.xp;
        }
        else
        {
            current = playerModel.getItemNum(key);
        }
        
        if (skin[key + "_numLabel"] != null) 
        {
            skin[key + "_numLabel"].text = String(current);
        }
	}
	
	protected function resourceNumChangedHandler(event:ItemValueEvent):void
	{
		var item:INumbericItemValue = event.target as INumbericItemValue;		
		updateResource(item.template.key);
	}
    
    protected function onClickSettingHandler():void
    {
        
    }
    
    protected function onClickGiftHandler():void
    {
        
    }
}
}





